﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Yss.KFlow.Deal;
using Yss.KFlow.Util;
using Yss.KFlow.Data;

namespace Yss.KFlow.Model
{
   /// <summary>
   /// 画板控件
   /// </summary>
    public partial class Flow_Canvas:UserControl  
    {

        FlowCanvasDeal fs = new FlowCanvasDeal();

        Flow_ToolBox ftb;
        /// <summary>
        /// 工具箱属性,设置画板绑定的工具箱
        /// </summary>
        public Flow_ToolBox Ftb
        {
            get { return ftb; }
            set { ftb = value; }
        }
        Flow_PropertyGrid fPG;
        /// <summary>
        /// 属性控件属性,设置绑定到画板的属性显示窗口
        /// </summary>
        public Flow_PropertyGrid FPG
        {
            get { return fPG; }
            set { fPG = value; }
        }
        /// <summary>
        /// 数据处理类
        /// </summary>
        internal FlowCanvasDeal Fs
        {
            get { return fs; }
            set { fs = value; }
        }
        private object lineTag;
        /// <summary>
        /// 绘图类型
        /// </summary>
        public FlowTypes YssType
        {
            get
            {
                return Fs.FlowType;
            }
            set
            {
                Fs.FlowType = value;
            }
        }
        /// <summary>
        /// 图片
        /// </summary>
        public Bitmap Pic
        {
            get
            {
                return Fs.Bitmap;
            }
            set
            {
                Fs.Bitmap = value;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public object LineTag
        {
            get
            {
                return lineTag;
            }
            set
            {
                lineTag = value;
            }
        }


        /// <summary>
        /// 花旦对象构造函数
        /// 支持拖动,双缓冲绘图
        /// 自动滚动条
        /// </summary>
        public Flow_Canvas()
        {
            InitializeComponent();
            this.AllowDrop = true;
            this.DoubleBuffered = true;
            this.AutoScroll = true;
            this.BackColor = Color.White;
            fs.YssSelectPic += new AfterSelectPic(Select);
            fs.YssShowForm += new AfterSelectPic(ShowForm);
            
      
        }
        /// <summary>
        /// 重载窗体绘画函数
        /// </summary>
        /// <param name="pe"></param>
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            Fs.OnPaint(this,pe);
        }
        /// <summary>
        /// 重载鼠标按下事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(MouseEventArgs e)
        {
            //if (Ftb == null)
            //{
            //    MessageBox.Show("程序运行出错");
            //    return;
            //}
            if (Ftb !=null && Ftb.Flag)
            {
                fs.FlowType = Ftb.FlowDraw;
                fs.Arrow = ftb.Arrow;
                Ftb.Flag = false;
            }
            base.OnMouseDown(e);            
            Fs.OnMouseDown(e);
            
        }
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="e"></param>
        //protected override void OnMouseLeave(EventArgs e)
        //{
        //    base.OnMouseLeave(e);

        //}
        /// <summary>
        /// 重载鼠标双击事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDoubleClick(MouseEventArgs e)
        {
            base.OnMouseDoubleClick(e);
            fs.OnMouseDoubleClick(e);

        }
        /// <summary>
        /// 重载鼠标移动事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseMove(MouseEventArgs e)
        {
           
            base.OnMouseMove(e);
            Fs.OnMouseMove(this , e);
           
        }
        /// <summary>
        /// 重载鼠标释放事件
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            Fs.OnMouseUp(e);
            this.Refresh();
        }
        /// <summary>
        /// 重载拖动进入事件
        /// </summary>
        /// <param name="drgevent"></param>
        protected override void OnDragEnter(DragEventArgs drgevent)
        {


            drgevent.Effect = drgevent.AllowedEffect;

            base.OnDragEnter(drgevent);
        }
        /// <summary>
        /// 重载拖完成时间
        /// </summary>
        /// <param name="drgevent"></param>
        protected override void OnDragDrop(DragEventArgs drgevent)
        {
            //拖动对象类型
            DragData yddd = (DragData)drgevent.Data.GetData("Yss.KFlow.Data.DragData");
            if (yddd == null)
            {
                return;
            }
            Fs.FlowType = FlowTypes.Pic;
            Fs.Bitmap =(Bitmap ) yddd.Pic;
            fs.YssTag = yddd.Sender;
            Point p = this.PointToClient(new Point(drgevent.X, drgevent.Y));
            Fs.AddOrDel(p);
            base.OnDragDrop(drgevent);
            this.Refresh();
            Fs.FlowType = FlowTypes.None;
        }

        private void YssPicture_KeyUp(object sender, KeyEventArgs e)
        {
            
            if (e.KeyCode == Keys.Delete)
            {
                if (fs.SelectedLine != null)
            {
                fs.LLines.Remove(fs.SelectedLine);
                fs.SelectedLine.Selected = false;
                fs.SelectedLine = null;
            }
            else if (fs.SelectPic != null)
            {
                for (int i = 0; i < fs.LLines.Count; i++)
                {
                    if (fs.LLines[i].StartPic == fs.SelectPic || fs.LLines[i].EndPic == fs.SelectPic)
                    {
                        fs.LLines.Remove(fs.LLines[i]);
                    }
                }
                fs.LPics.Remove(fs.SelectPic);
                fs.SelectPic.Selected = false;
                fs.SelectPic = null;
            }
            }
            this.Refresh();
        }
        private void Select(Shape yp)
        {
            //BaseTool bt = yp.Tag as BaseTool;
            if (this.FPG != null)
            {
                FPG . PG . SelectedObject = yp . Tag;
               // FPG.Refresh();
            }
            this . Refresh ( );

            //flowPG.PG.SelectedObject = bt.Tag;
           

        }
        private void ShowForm(Shape yp)
        {
            BaseTool bt = yp.Tag as BaseTool;
            Form f = bt.Tag as Form;
            if (f != null)
            {
                this.Refresh();
                f.ShowDialog();
            }

        }
        protected override void OnKeyDown ( KeyEventArgs e )
        {
            this . Focus ( );
            fs . OnKeyDown ( e );
            base . OnKeyDown ( e );
           // this . Size = new Size ( this . Size . Width+1 , this . Size . Height+1 );
            this . Refresh ( );
        }

        
        //protected override bool ProcessCmdKey ( ref Message msg , Keys keyData )
        //{
        //    if ( keyData == Keys . Down )
        //    {
        //       // fs . OnKeyDown ( keyData );
        //        return false;
        //       // fs . OnKeyDown ( keyData );
        //    }

        //    return base . ProcessCmdKey ( ref msg , keyData );
        //}

        /// <summary>
        /// 重写基类方法
        /// 确定指定的键是常规输入键还是需要预处理的特殊键。
        /// </summary>
        /// <param name="keyData">输入的键</param>
        /// <returns>是否为输入键</returns>
        protected override bool IsInputKey ( System . Windows . Forms . Keys keyData )
        {
            return true;
        }
    }
}
